package yxy.game.pm2.active.command.ghost;

import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actghost;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_神魔卡;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;

/**
 * 神魔卡_抽将 奖池信息
 * @author Dell
 *
 */
@MessageType(proto = Command.ActGhostGachaHeroJackpot, classtype = ActiveMessage.ActGhostGachaHeroJackpotRequest.class)
public class CMD_Active_神魔卡_抽将_Jackpot extends Command<ActiveMessage.ActGhostGachaHeroJackpotRequest> {

	private static final Logger logger = Logger.create(CMD_Active_神魔卡_抽将_Jackpot.class);
	
	@Override
	public void execute(MessageSource messageSource, ActiveMessage.ActGhostGachaHeroJackpotRequest request) throws Exception {
		String playercode = messageSource.getTag();
		ActiveMessage.ActGhostGachaHeroJackpotResponse.Builder response = ActiveMessage.ActGhostGachaHeroJackpotResponse.newBuilder();
		
		ActiveModule_神魔卡 am = ActiveModule_神魔卡.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(am.getGhostmap().size() == 0) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		actghost ag = ActDataCenter.getData(101, actghost.class, playercode);
		int[] curtypes = ag.getRewardtype();
		int[] curids = ag.getRewardid();
		int[] curnums = ag.getRewardnum();

		Collection<Currency> curs = Currency.create(curtypes, curids, curnums);
		Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);

		response.addAllDisplay(items);
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActGhostGachaHeroJackpotResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
